/*
 * spiDriver.c
 *
 * Created: 04.10.2012 13:50:04
 *  Author: vegardvo
 */ 

#include "spiDriver.h"
#include "definitions.h"
#include <avr/io.h>

void spiDriver_init()
{
	SET_BIT(SPCR, SPE);		// set SPI Enable
	SET_BIT(SPCR, MSTR);	// select master SPI mode
	SET_BIT(DDRB, PB5);		// set MOSI as output
	SET_BIT(DDRB, PB7);		// set SCK as output
	SET_BIT(DDRB, PB4);		// set ~SS as output
	SET_BIT(SPCR, SPR0);	// set SCK rate = f_osc/16
	SET_BIT(PORTB,PB4);		// set SS to 1
}

void spiDriver_transmit(char data)
{
	//CLEAR_BIT(PORTB, PB4);
	SPDR = data;
	while(!(SPSR & (1<<SPIF)))
	{
	}
}

char spiDriver_receive()
{
	spiDriver_transmit(0xFF);
	while(!(SPSR & (1<<SPIF)))
	{
	}
	return SPDR;
}